var PATH_PREFIX = "";
var flag = false;
function setPathPrefix(path) {
	PATH_PREFIX = path;
}

var roleId = $("#roleId").val();


var $resourceList = $('#resourceList');
var $selectedList = $('#selectedList');
$(function(){
	
	// 项目经理下拉选择
	WASP_WIDGET.triggerSelectOnEmpId("#mgrId", {multiple: false}, {cleanDele: true});
	WASP_WIDGET.triggerSelectOnEmpId("#selectedMgrId", {multiple: false}, {cleanDele: true});
	//注册清空事件
    WASP_WIDGET.registerResetClearEvent();
	
	$resourceList.jqGrid({
        url: PATH_PREFIX +"queryResourceForSel.do",
        caption: '待选资源列表',
        datatype: "json",
        postData : {
        	'sp[roleId]': roleId
        },
		colNames:["资源Id","资源名称","项目经理"],
		colModel:[  {name:'resId',index:'resId',key:true, hidden:true},
					{name:'resName',index:'resName',resizable:true,align:'left'},
					{name:'mgrNames',index:'mgrNames',resizable:true, hidden:true}
				],    
        rowNum: 20,
        rowList: [20, 30, 100, 200, 500],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: {
        	        search: "search", 
        	        page: "pageNo",
        	        rows: "limit" 
        	       },
        height: '350px',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },
        multiselect: true,
        pager: "#resourcePage",
        viewrecords: true,
        hidegrid: false,
		gridComplete: function(){
			
		}
	});
    $resourceList.navGrid('#resourcePage', { edit: false, add: false, del: false, search: false, refreshstate: 'current' });
    $resourceList.jqGrid('setFrozenColumns');
    jqGridResize($resourceList);
    
    
    
    $selectedList.jqGrid({
        url: PATH_PREFIX+'querySelectedResource.do',
        caption: '已选资源列表',
        datatype: "json",
        postData : {
        	'sp[roleId]': roleId
        },
		colNames:["资源Id","资源名称","项目经理"],
		colModel:[  {name:'resId',index:'resId',key:true, hidden:true},
					{name:'resName',index:'resName',resizable:true,align:'left'},
					{name:'mgrNames',index:'mgrNames',resizable:true, hidden:true}
		],
        rowNum: 20,
        rowList: [20, 30, 100, 200, 500],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: {
        	        search: "search", 
        	        page: "pageNo",
        	        rows: "limit" 
        	       },
        height: '350px',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },
        multiselect: true,
        pager: "#selectedPage",
        viewrecords: true,
        hidegrid: false,
		gridComplete: function(){
			
		}
	});
    $selectedList.navGrid('#selectedPage', { edit: false, add: false, del: false, search: false, refreshstate: 'current' });
    $selectedList.jqGrid('setFrozenColumns');
    jqGridResize($selectedList);
    
    
    
    // 修复分页工具栏宽度在较短的列表下的bug
    fixPagerWidth();
});

function fixPagerWidth() {
	$("#resourcePage_center").attr("style", "white-space: pre;width: 297px!important;");
	$("#selectedPage_center").attr("style", "white-space: pre;width: 297px!important;");
	

	$("#resourcePage_right").attr("style", "width: 170px!important;");
	$("#selectedPage_right").attr("style", "width: 170px!important;");
}

/**
 * 搜索待选资源
 */
function queryResource(flag) {
	//获取角色名称
	var resName = $.trim($("#resName").val());
	var mgrId = $.trim($("#mgrId").val());
	var postData = $resourceList.jqGrid("getGridParam", "postData");
	$.extend(postData, {
		'sp[resName]':resName,
		'sp[mgrId]':mgrId,
	});
	if (flag) {
		$resourceList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
	} else {
		$resourceList.trigger("reloadGrid"); //重新载入Grid表格
	}
};


/**
 * 搜索已选资源
 */
function querySelected(flag) {
	//获取角色名称
	var selectedResName = $.trim($("#selectedResName").val());
	var selectedMgrId = $.trim($("#selectedMgrId").val());
	var postData = $selectedList.jqGrid("getGridParam", "postData");
    $.extend(postData, {
        'sp[resName]':selectedResName,
        'sp[mgrId]':selectedMgrId
    });
    if (flag) {
    	$selectedList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
    } else {
    	$selectedList.trigger("reloadGrid"); //重新载入Grid表格
    }
};


/**
 * 右移加入已选资源，添加数据权限
 * @param isAll
 */
function moveRight(isAll) {
	var checkedResource = $resourceList.jqGrid('getGridParam', 'selarrrow');
	if (isAll != 'Y') {
		// 非全选，校验是否选中数据
		if (0 == checkedResource.length) {
			ctools.alert("请选择左侧待选资源","","warning");
			return;
		}
		
		checkedResource = checkedResource.join();
	} else {
		checkedResource = "";
	}
	
	$.ajax({
		type : "POST",
		dataType : "json",
		url : PATH_PREFIX + "addResource.do",
		data: {
			roleId : roleId,
			isAll : isAll,
			resIds : checkedResource
		},
		success: function(data){
			if (data.success) {
				ctools.alert(data.message,"","success");
				
				$resourceList.trigger("reloadGrid", [{ page: 1 }]);
				$selectedList.trigger("reloadGrid", [{ page: 1 }]);
			} else {
				swal(data.message, "", "warning");
			}
		},
		error:function(xhr){
		   sweetAlertAjaxError(xhr);
		}
	});
}

/**
 * 左移取消已选，移除数据权限
 * @param isAll
 */
function moveLeft(isAll) {
	var checkedResource = $selectedList.jqGrid('getGridParam', 'selarrrow');
	if (isAll != 'Y') {
		// 非全选，校验是否选中数据
		if (0 == checkedResource.length) {
			ctools.alert("请选择右侧已选资源","","warning");
			return;
		}
		
		checkedResource = checkedResource.join();
	} else {
		checkedResource = "";
	}
	
	$.ajax({
		type : "POST",
		dataType : "json",
		url : PATH_PREFIX + "removeResource.do",
		data: {
			roleId : roleId,
			isAll : isAll,
			resIds : checkedResource
		},
		success: function(data){
			if (data.success) {
				ctools.alert(data.message,"","success");
				
				$resourceList.trigger("reloadGrid", [{ page: 1 }]);
				$selectedList.trigger("reloadGrid", [{ page: 1 }]);
			} else {
				swal(data.message, "", "warning");
			}
		},
		error:function(xhr){
		   sweetAlertAjaxError(xhr);
		}
	});
}